<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use App\Models\SystemSetting;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        // 同步系统设置到Laravel配置
        $this->syncSystemSettingsToConfig();
        
        // 共享系统设置到所有视图
        $this->shareSystemSettingsToViews();
    }

    /**
     * 同步系统设置到Laravel配置
     */
    private function syncSystemSettingsToConfig(): void
    {
        try {
            // 获取所有系统设置
            $settings = SystemSetting::all();
            
            foreach ($settings as $setting) {
                // 将设置同步到config中，使用点号分隔的键名
                $configKey = "system.{$setting->key}";
                Config::set($configKey, $setting->value);
                
                // 如果是特殊的系统配置，同步到对应的Laravel配置
                $this->syncSpecialConfigs($setting);
            }
        } catch (\Exception $e) {
            // 如果数据库还未迁移，忽略错误
            \Log::info('SystemSetting sync skipped: ' . $e->getMessage());
        }
    }

    /**
     * 同步特殊配置到Laravel配置系统
     */
    private function syncSpecialConfigs($setting): void
    {
        switch ($setting->key) {
            case 'app_name':
                Config::set('app.name', $setting->value);
                break;
            case 'app_url':
                Config::set('app.url', $setting->value);
                break;
            case 'mail_host':
                Config::set('mail.mailers.smtp.host', $setting->value);
                break;
            case 'mail_port':
                Config::set('mail.mailers.smtp.port', $setting->value);
                break;
            case 'mail_username':
                Config::set('mail.mailers.smtp.username', $setting->value);
                break;
            case 'mail_password':
                Config::set('mail.mailers.smtp.password', $setting->value);
                break;
            case 'mail_encryption':
                Config::set('mail.mailers.smtp.encryption', $setting->value);
                break;
            case 'mail_from_address':
                Config::set('mail.from.address', $setting->value);
                break;
            case 'mail_from_name':
                Config::set('mail.from.name', $setting->value);
                break;
        }
    }

    /**
     * 共享系统设置到所有视图
     */
    private function shareSystemSettingsToViews(): void
    {
        try {
            // 获取公开的系统设置
            $publicSettings = SystemSetting::getPublicSettings();
            
            // 共享到所有视图
            View::share('systemSettings', $publicSettings);
            
            // 共享常用设置的快捷访问
            View::share('siteName', $publicSettings['site_name'] ?? config('app.name'));
            View::share('siteDescription', $publicSettings['site_description'] ?? '');
            View::share('siteLogo', $publicSettings['site_logo'] ?? '');
            View::share('siteKeywords', $publicSettings['site_keywords'] ?? '');
        } catch (\Exception $e) {
            // 如果数据库还未迁移，使用默认值
            View::share('systemSettings', []);
            View::share('siteName', config('app.name'));
            View::share('siteDescription', '');
            View::share('siteLogo', '');
            View::share('siteKeywords', '');
        }
    }
}
